import { getUserList, getRoleList, getResoList, searchUser } from "@/api/userManage/api"
let state = {
    userList: {},
    roleList: {},
    resoList: {}
}
const actions = {
    // 获取用户列表信息
    async actionGetUserList({ commit }, pageInfo) {
        const res = await getUserList(pageInfo)
        console.log('用户列表信息分页获取可注释', res)
        commit('SET_USERLIST', res)
    },
    // 获取角色列表信息
    async actionGetRoleList({ commit }, pageInfo) {
        const res = await getRoleList(pageInfo)
        console.log('角色列表信息分页获取可注释', res)
        commit('SET_ROLELIST', res)
    },
    // 获取资源列表信息
    async actionGetResoList({ commit }, pageInfo) {
        const res = await getResoList(pageInfo)
        console.log('资源列表信息分页获取可注释', res)
        if(res.code==200){
            commit('SET_RESOLIST', res)
        }else{
            return
        }
    },
    // 查询用户列表信息
    async actionsearchUserList({ commit }, pageInfo) {
        const res = await searchUser(pageInfo)
        console.log('用户列表信息分页获取可注释', res)
        commit('SET_USERLIST', res)
    },
}
const mutations = {
    // 将用户列表信息存入vuex
    SET_USERLIST: (state, res) => {
        state.userList = res
    },
    // 将角色列表信息存入vuex
    SET_ROLELIST: (state, res) => {
        state.roleList = res
    },
    // 将资源列表信息存入vuex
    SET_RESOLIST: (state, res) => {
        state.resoList = res
    }
}
const getters = {
    filterResoList:(state) =>{
        function getnewarr(testArr) {
            let aaa = [];
            testArr.forEach((item) => {
                if (item.children) {
                    let children = getnewarr(item.children);
                    aaa.push({
                        id: item.id,
                        label: item.rname,
                        children: children,
                    });
                } else {
                    aaa.push({
                        id: item.id,
                        label: item.rname,
                    });
                }
            });
            return aaa;
        }
        const state2 = JSON.parse(JSON.stringify(state))
        console.log(getnewarr(state2.resoList.data.records))
        return getnewarr(state2.resoList.data.records)
    }
}
export default {
    namespaced: true,
    state,
    actions,
    mutations,
    getters
}